// 手写深拷贝，考虑循环引用
const obj = {
  a: 1,
  arr: [],
  _obj: {},
};

obj.arr.push(obj);
obj.c = obj;

function cloneDeep(obj, wm = new WeakMap()) {
  if (wm.has(obj)) return wm.get(obj);
  const result = Array.isArray(obj) ? [] : {};
  wm.set(obj, result);
  Object.entries(obj).forEach((item) => {
    const [key, value] = item;
    if (typeof value === "object") {
      result[key] = cloneDeep(value, wm);
    } else {
      result[key] = value;
    }
  });
  return result;
}

const _cloneObj = cloneDeep(obj);

console.log(_cloneObj);
